package com.qf.zsq.service.impl;


import com.qf.entity.GoodsEntity;
import com.qf.entity.GoodsTypeEntity;
import com.qf.mapper.GoodsMapper;
import com.qf.pojo.GoodsPojo;
import com.qf.pojo.GoodsTypePojo;
import com.qf.zsq.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    /**
     * 根据条件查询商品列表
     * @param ge
     * @return
     */
    public List<GoodsEntity> queryGoodsList(GoodsEntity ge){

        List<GoodsEntity> list = null;

        //查询条件的转换
        GoodsPojo gp = entityToPojo(ge);

        //查询我们的商品列表
        List<GoodsPojo> goodsPojos = goodsMapper.queryGoodsList(gp);

        //实体类转换
        list = pojoListToEntiyList(goodsPojos);

        return list;
    }

    /**
     * 查询商品类别
     * @return
     */
    public List<GoodsTypeEntity> queryGoodsTypeList(){

        List<GoodsTypeEntity> listEntitys;

        //查询到商品类别列表,然后进行转换
        List<GoodsTypePojo> list = goodsMapper.queryGoodsTypeList();
        listEntitys = typeListPojoToEntity(list);

        return listEntitys;
    }

    @Override
    public GoodsEntity queryGoodsDetail(String goodsId) {

        GoodsPojo gp = goodsMapper.queryGoodsDetail(goodsId);
        return pojoToEntity(gp);
    }

    /**
     * 商品类别pojo转成entity ; 转list
     * @param list
     * @return
     */
    private List<GoodsTypeEntity> typeListPojoToEntity(List<GoodsTypePojo> list) {

        List<GoodsTypeEntity> lt = null;

        if(null != list && list.size() > 0){
            lt = new ArrayList<>();
            for(GoodsTypePojo gtp : list){
                lt.add(typePojoToEntity(gtp));
            }
            return lt;
        }

        return lt;
    }

    //将pojo转成entity
    private GoodsEntity pojoToEntity(GoodsPojo gp){

        GoodsEntity ge = new GoodsEntity();

        ge.setGoodsId(gp.getGid());
        ge.setGoodsName(gp.getGname());
        ge.setGoodsDesc(gp.getGdesc());
        ge.setGoodsPrice(gp.getGprice());
        ge.setGoodsDiscount(gp.getGdiscount());
        ge.setGoodsImage(gp.getGimage());
        ge.setGoodsIsdelete(gp.getIsdelete());
        ge.setGoodsLabel(gp.getGlabel());
        ge.setGoodsSex(gp.getGsex());

        if(null == gp.getGtype()){
            ge.setGoodsType(null);
        }else{
            GoodsTypeEntity gte = typePojoToEntity(gp.getGtype());
            ge.setGoodsType(gte);
        }

        return ge;

    }

    //商品entity转pojo ; 类型转换
    private GoodsPojo entityToPojo(GoodsEntity ge){

        if(null == ge){
            return null;
        }

        GoodsPojo gp = new GoodsPojo();

        gp.setGid(ge.getGoodsId());
        gp.setGdesc(ge.getGoodsDesc());
        gp.setGdiscount(ge.getGoodsDiscount());
        gp.setGimage(ge.getGoodsImage());
        gp.setGlabel(ge.getGoodsLabel());
        gp.setGname(ge.getGoodsName());
        gp.setGprice(ge.getGoodsPrice());
        gp.setGsex(ge.getGoodsSex());
        gp.setIsdelete(ge.getGoodsIsdelete());
        gp.setTid(ge.getGoodsTid());//我只改了前台传后台的地方,后台传前台没有改,报了错去排除一下

        //商品类型
        if(null == ge.getGoodsType()){
            gp.setGtype(null);
        }else{
            GoodsTypePojo gtp = typeEntityToPojo(ge.getGoodsType());
            gp.setGtype(gtp);
        }

        return gp;
    }

    //pojo集合转成entity集合
    private List<GoodsEntity> pojoListToEntiyList(List<GoodsPojo> list){

        if(null != list && list.size() > 0){
            List<GoodsEntity> listEntity = new ArrayList<GoodsEntity>();

            GoodsEntity ge;
            for(GoodsPojo gp : list){
                ge = pojoToEntity(gp);
                listEntity.add(ge);
            }

            return listEntity;
        }

        return null;
    }

    //把商品类别的pojo转成entity
    private GoodsTypeEntity typePojoToEntity(GoodsTypePojo gp){
        GoodsTypeEntity gte = new GoodsTypeEntity();

        gte.setTypeId(gp.getTid());
        gte.setTypeDesc(gp.getTdesc());
        gte.setTypeImage(gp.getTimage());
        gte.setTypeName(gp.getTname());

        return gte;
    }

    //把商品类别的entity转成pojo
    private GoodsTypePojo typeEntityToPojo(GoodsTypeEntity gte){
        GoodsTypePojo gtp = new GoodsTypePojo();

        gtp.setTid(gte.getTypeId());
        gtp.setTname(gte.getTypeName());
        gtp.setTdesc(gte.getTypeDesc());
        gtp.setTimage(gte.getTypeImage());

        return gtp;
    }
}
